$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'users/user/list',
        datatype: "json",
        colModel: [
			{ label: '用户ID', name: 'id', index: "user_id", width: 70, key: true,hidden:true },
			{ label: '电话', name: 'mobileNumber', width: 35 },
            { label: '用户姓名', name: 'userName', width: 45 },
			{ label: '推荐人', name: 'referralName', width: 35 },
            { label: '用户等级', name: 'identityName', sortable: false, width: 35 },
			{ label: '创建时间', name: 'createTime', width: 55},
            { label: '状态', name: 'status', width: 60,
                formatter: function(value, options, row){
                    if (value === "normal") {
                        return '<span class="label label-success">正常</span>';
                    } else if (value === "freeze"){
                        return '<span class="label label-danger">冻结</span>';
                    } else if (value ==="lock"){
                        return '<span class="label label-default">锁定</span>';
                    } else {
                        return value;
                    }
                }
             }
        ],
		viewrecords: true,
        height: 430,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.result",
            page: "page.pageNo",
            total: "page.totalPages",
            records: "page.total"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });

});


var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            userName: null,
            mobileNumber:null,
            startDate:null,
            endDate:null,
            identity:""
        },
        showList: true,
        infoList: false,
        title:null,
        userDetalTitle:null,
        accouts:[],
        identity:[],
        user:{
            status:1,
            bao:0,
            zai:0,
            zhong:0,
            ling:0,
            zuan:0,
            yuan:0
        }
    },
    // 获取用户身份集合
    created:function(){
        $.get(baseURL + "users/user/getIdentitys", function(r){vm.identity = r.data;});
    },
    methods: {
        query: function () {
            vm.reload();
        },
        info: function(){
            var grid = $("#jqGrid");
            var userId = grid.getGridParam("selrow");
            if(!userId){
                alert("请选择一条记录");
                return ;
            }

            vm.showList = false;
            vm.infoList = true;
            vm.userDetalTitle = "用户详情";

            vm.getUser(userId);
        },
        getUser: function(userId){
            $.get(baseURL + "users/user/info/"+userId, function(r){

                vm.user = r.user;
                vm.accouts = r.accouts;
                vm.user.password = null;

            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'realName': vm.q.userName,'mobileNumber':vm.q.mobileNumber,'startDate':vm.q.startDate,'endDate':vm.q.endDate,'identity':vm.q.identity},
                page:page
            }).trigger("reloadGrid");
        },
        infoReload: function () {
            vm.showList = true;
            vm.infoList = false;
        },
        accoutDetail:function (userId,accountTypeCode) {
            localStorage.setItem('userId', userId);
            localStorage.setItem('accountTypeCode', accountTypeCode);

            layer.open({
                type: 2,
                skin: 'layui-layer-molv',
                title: "变动记录",
                shadeClose: false,
                area: ['700px', '450px'],
                fixed: false, //不固定
                maxmin: true,
                content: '../users/userAccout.html'
            });
        },
        freeze:function () {// 冻结
            var grid = $("#jqGrid");
            var userId = grid.getGridParam("selrow");
            if(!userId){
                alert("请选择一条记录");
                return ;
            }
            $.ajax({
                type: "POST",
                url: baseURL + "users/user/freezeUser",
                contentType: "application/json",
                data: JSON.stringify({id:userId}),
                success: function(r){
                    if(r.code === 'success'){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.message);
                    }
                },
                error:function (r) {
                    alert(r.responseJSON.message);
                }
            });
        },
        unFreeze:function () {// 解冻
            var grid = $("#jqGrid");
            var userId = grid.getGridParam("selrow");
            if(!userId){
                alert("请选择一条记录");
                return ;
            }
            $.ajax({
                type: "POST",
                url: baseURL + "users/user/unFreezeUser",
                contentType: "application/json",
                data: JSON.stringify({id:userId}),
                success: function(r){
                    if(r.code === 'success'){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.message);
                    }
                },
                error:function (r) {
                    alert(r.responseJSON.message);
                }
            });
        },
        update:function () {
            var grid = $("#jqGrid");
            var userId = grid.getGridParam("selrow");
            if(!userId){
                alert("请选择一条记录");
                return ;
            }
            vm.showList = false;
            vm.infoList = false;
            vm.userDetalTitle = "修改用户";
            vm.getUser(userId);
        },
        saveOrUpdate: function () {
            var url = "users/user/update";
            if (vm.validateData()) {
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.user),
                    success: function(r){
                        if(r.code === 'success'){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.message);
                        }
                    },
                    error:function (r) {
                        alert(r.responseJSON.message);
                    }
                });
            } else {
                return
            }
        },
        validateData:function () {
            if (!mobileReg(vm.user.mobileNumber)) {
                showTips("手机号输入不合法", '#mobile');
                return false;
            }
            return true;
        }
    }
});
function validateNumber(num) {
    var re = /^\d+(?=\.{0,1}\d+$|$)/;
    if (num==0){
        return true;
    }
    if (isBlank(num)) {
        return false;
    }
    if (!re.test(num)) {
        return false;
    }
    return true;
}

function mobileReg(m) {
    var reg = /^(((13[0-9]{1})|14[5|7]|(15[0|0|1|2|3|5|6|7|8|9]{1})|16[6]|17[0|1|2|3|5|6|7|8|9]|(18[0-9]{1})|19[9])+\d{8})$/;
    if (isBlank(m)) {
        return false;
    }
    if (!reg.test(m)){
        return false;
    }
    return true;
}
layui.use('laydate', function(){
    var laydate = layui.laydate;
    laydate.render({
        elem: '#startDate',
        format:'yyyy-MM-dd HH:mm:ss',
        done: function(value, date, endDate){
            vm.q.startDate = value;
        }
    });
    laydate.render({
        elem: '#endDate',
        format:'yyyy-MM-dd HH:mm:ss',
        done: function(value, date, endDate){
            vm.q.endDate = value;
        }
    });
});